<?php

namespace app\rongcui\controller;

use app\rongcui\model\AreasModel;
use app\rongcui\model\CustomerNatureModel;
use app\rongcui\model\RongcuiAgencyModel;
use app\rongcui\model\RongcuiAgencyUserModel;
use cmf\controller\AdminBaseController;

class AgencyController extends AdminBaseController
{
    public function index()
    {
        $where = [];
//        $uid = cmf_get_current_admin_id();
//        if ($uid != 1) {
//            $users = implode(',', getUsersForRole($uid));
//            $where['agency.uid'] = ['in', $users];
//        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|agency.name|address|remark|areas.name|citys.name'] = ['like', "%$keyword%"];
        }
        if (input('type', 0, 'intval')) {
            $type = input('type', 0, 'intval');
            $where['agency.type'] = $type;
        }
        if (input('relations', 0, 'intval')) {
            $type = input('relations', 0, 'intval');
            $where['agency.relations'] = $type - 1;
        }
        $rongcuiAgencyModel = new RongcuiAgencyModel();
        $lists = $rongcuiAgencyModel
            ->alias('agency')
            ->join('__USER__ user', 'agency.uid = user.id')
            ->join('__AREAS__ areas', 'areas.areaid = agency.area div 10000 * 10000')
            ->join('__AREAS__ citys', 'citys.areaid = agency.area div 100 * 100')
            ->join('__CUSTOMER_NATURE__ nature','agency.type = nature.id and nature.type = 10')
            ->where($where)
            ->field('agency.*,user.user_nickname,areas.name aname,citys.name cname,nature.name tname')
            ->order('agency.id desc')
            ->paginate();

        $customerNatureModel = new CustomerNatureModel();
        $types = $customerNatureModel
            ->where([
                'type' => 10,
                'is_del' => 0,
            ])
            ->order('list_order')
            ->select();

        $this->assign('types', $types);
        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();
            $arrData['uid'] = cmf_get_current_admin_id();
            if ($arrData['city']) {
                $arrData['area'] = $arrData['city'];
            }
            if ($arrData['qu']) {
                $arrData['area'] = $arrData['qu'];
            }

            $rongcuiAgencyModel = new RongcuiAgencyModel();
            $flag = $rongcuiAgencyModel
                ->allowField(true)
                ->validate(true)
                ->save($arrData);
            if ($flag !== false) {
                $this->success('添加成功！', cmf_url('index'));
            } else {
                $this->error($rongcuiAgencyModel->getError());
            }
        }

        $customerNatureModel = new CustomerNatureModel();
        $types = $customerNatureModel
            ->where([
                'type' => 10,
                'is_del' => 0,
            ])
            ->order('list_order')
            ->select();

        $areasModel = new AreasModel();
        $areas = $areasModel
            ->where([
                'level' => 1
            ])
            ->select();

        $this->assign('areas', $areas);
        $this->assign('types', $types);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('访问错误！');
        }
        $rongcuiAgencyModel = new RongcuiAgencyModel();
        $result = $rongcuiAgencyModel
            ->find($id);
        if (!$result) {
            $this->error('访问错误！');
        }
        if ($this->request->isPost()) {
            $arrData = $this->request->post();
            if ($arrData['city']) {
                $arrData['area'] = $arrData['city'];
            }
            if ($arrData['qu']) {
                $arrData['area'] = $arrData['qu'];
            }

            $rongcuiAgencyModel = new RongcuiAgencyModel();
            $flag = $rongcuiAgencyModel
                ->allowField(true)
                ->validate(true)
                ->save($arrData, ['id' => $id]);
            if ($flag !== false) {
                $this->success('保存成功！', cmf_url('index'));
            } else {
                $this->error($rongcuiAgencyModel->getError());
            }
        }

        $customerNatureModel = new CustomerNatureModel();
        $types = $customerNatureModel
            ->where([
                'type' => 10,
                'is_del' => 0,
            ])
            ->order('list_order')
            ->select();

        $areasModel = new AreasModel();
        $areas = $areasModel
            ->where([
                'level' => 1
            ])
            ->select();

        $this->assign('areas', $areas);
        $this->assign('types', $types);
        $this->assign('result', $result);
        return $this->fetch();
    }

    function select()
    {
        $id = $this->request->param('id', 0, 'intval');
        $where = [];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|agency.name|address|remark|areas.name|citys.name'] = ['like', "%$keyword%"];
        }
        if (input('type', 0, 'intval')) {
            $type = input('type', 0, 'intval');
            $where['agency.type'] = $type;
        }
        if (input('relations', 0, 'intval')) {
            $type = input('relations', 0, 'intval');
            $where['agency.relations'] = $type - 1;
        }
        $rongcuiAgencyModel = new RongcuiAgencyModel();
        $lists = $rongcuiAgencyModel
            ->alias('agency')
            ->join('__USER__ user', 'agency.uid = user.id')
            ->join('__AREAS__ areas', 'areas.areaid = agency.area div 10000 * 10000')
            ->join('__AREAS__ citys', 'citys.areaid = agency.area div 100 * 100')
            ->join('__CUSTOMER_NATURE__ nature','agency.type = nature.id and nature.type = 10')
            ->where($where)
            ->field('agency.*,user.user_nickname,areas.name aname,citys.name cname,nature.name tname')
            ->order('agency.id desc')
            ->paginate();

        $customerNatureModel = new CustomerNatureModel();
        $types = $customerNatureModel
            ->where([
                'type' => 10,
                'is_del' => 0,
            ])
            ->order('list_order')
            ->select();

        $this->assign('lists', $lists);
        $this->assign('id', $id);
        $this->assign('types', $types);
        $this->assign('page', $lists->render());
        return $this->fetch();
    }

    function user_lists()
    {
        $where = [];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['agency_user.uid'] = ['in', $users];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user.user_nickname|agency.name|address|tel|phone|agency_user.remark|agency_user.name'] = ['like', "%$keyword%"];
        }
        $aid = 0;
        if (input('aid', 0, 'intval')) {
            $aid = input('aid', 0, 'intval');
            $where['agency_user.pid'] = $aid;
        }
        if (input('type', 0, 'intval')) {
            $type = input('type', 0, 'intval');
            $where['agency.type'] = $type;
        }
        $rongcuiAgencyUserModel = new RongcuiAgencyUserModel();
        $lists = $rongcuiAgencyUserModel
            ->alias('agency_user')
            ->join('__USER__ user','agency_user.uid = user.id')
            ->join('__RONGCUI_AGENCY__ agency','agency_user.pid = agency.id')
            ->join('__CUSTOMER_NATURE__ nature','agency.type = nature.id and nature.type = 10')
            ->where($where)
            ->field('agency_user.*,user.user_nickname,agency.name aname,agency.type,nature.name tname')
            ->paginate();

        $customerNatureModel = new CustomerNatureModel();
        $types = $customerNatureModel
            ->where([
                'type' => 10,
                'is_del' => 0,
            ])
            ->order('list_order')
            ->select();

        $this->assign('types', $types);
        $this->assign('aid', $aid);
        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());
        return $this->fetch();
    }

    function user_add()
    {
        $aid = input('aid', 0, 'intval');
        if ($this->request->isPost()) {
            $arrData = $this->request->post();
            $arrData['uid'] = cmf_get_current_admin_id();
            if($arrData['aid']) {
                $arrData['pid'] = $arrData['aid'];
            }

            $rongcuiAgencyUserModel = new RongcuiAgencyUserModel();
            $flag = $rongcuiAgencyUserModel->allowField(true)->validate(true)->save($arrData);
            if($flag !== false) {
                $this->success('添加成功！',cmf_url('user_lists',['aid' => $aid]));
            }else{
                $this->error($rongcuiAgencyUserModel->getError());
            }
        }
        $this->assign('aid', $aid);
        return $this->fetch();
    }

    function user_edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $aid = input('aid', 0, 'intval');
        if (!$id) {
            $this->error('访问错误！');
        }
        $rongcuiAgencyUserModel = new RongcuiAgencyUserModel();
        $result = $rongcuiAgencyUserModel
            ->alias('agency_user')
            ->join('__RONGCUI_AGENCY__ agency','agency_user.pid = agency.id')
            ->field('agency_user.*,agency.name aname')
            ->find($id);
        if (!$result) {
            $this->error('访问错误！');
        }
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $flag = $rongcuiAgencyUserModel->allowField(true)->validate(true)->save($arrData,[
                'id'=>$id
            ]);
            if($flag !== false) {
                $this->success('保存成功！',cmf_url('user_lists',['aid' => $aid]));
            }else{
                $this->error($rongcuiAgencyUserModel->getError());
            }
        }

        $this->assign('aid', $aid);
        $this->assign('result',$result);
        return $this->fetch();
    }
}